using System;
using System.IO;
namespace Servlets.Api {
	public class WebFile {
		public ContentType ContentType {get;private set;}
		public byte[] Content {get; private set;}
		public WebFile(ContentType contentType, byte[] bytes) {
			this.ContentType = contentType;
			this.Content = bytes;
		}
		
		
		/// <summary>
		/// Writes the contents of the file to the given path. If the file already exists the file will be overridden
		/// </summary>
		/// <param name="path">
		/// A <see cref="System.String"/>
		/// </param>
		public void SaveToFile(string path) {
			var stream = File.Create(path);
			SaveToStream(stream);
			stream.Close();
		}
		
		/// <summary>
		/// Writes the contents of the file to the given stream then flushes the stream
		/// </summary>
		/// <param name="s">
		/// A <see cref="Stream"/>
		/// </param>
		public void SaveToStream(Stream s) {
			foreach(byte b in Content) {
				s.WriteByte(b);
			}
			s.Flush();
		}
	}
}

